<template>
  <div class="magic-container">
    <!-- 动态背景 -->
    <div class="magic-bg">
      <div class="gradient-circle pink"></div>
      <div class="gradient-circle blue"></div>
      <div class="gradient-circle purple"></div>
    </div>

    <!-- 表单主体 -->
    <div class="form-container">
      <div class="form-card">
        <div class="form-header">
          <h2 class="form-title">
            {{ route.query?.id ? '修改' : '创建' }} {{ SPACE_TYPE_MAP[spaceType] }}
          </h2>
          <div class="title-line"></div>
        </div>

        <a-form name="spaceForm" layout="vertical" :model="spaceForm" @finish="handleSubmit">
          <div class="form-content">
            <a-form-item name="spaceName" label="空间名称" class="form-item">
              <template #label>
                <span class="form-label">空间名称</span>
              </template>
              <a-input
                v-model:value="spaceForm.spaceName"
                placeholder="请输入空间名称"
                class="custom-input"
                allow-clear
              />
            </a-form-item>

            <a-form-item name="spaceLevel" label="空间级别" class="form-item">
              <template #label>
                <span class="form-label">空间级别</span>
              </template>
              <a-select
                v-model:value="spaceForm.spaceLevel"
                placeholder="请选择空间级别"
                :options="SPACE_LEVEL_OPTIONS"
                class="custom-select"
                allow-clear
              />
            </a-form-item>

            <a-form-item class="form-item">
              <a-button type="primary" html-type="submit" :loading="loading" class="submit-btn">
                {{ route.query?.id ? '保存修改' : '立即创建' }}
              </a-button>
            </a-form-item>
          </div>
        </a-form>

        <a-card class="info-card">
          <template #title>
            <div class="card-title">
              <span class="icon">✨</span>
              <span>空间级别说明</span>
            </div>
          </template>
          <a-typography-paragraph class="card-text">
            * 目前仅支持开通普通版，如需升级空间，请联系
            <a href="http://101.42.137.75" target="_blank" class="contact-link">KSCN</a>
          </a-typography-paragraph>
          <a-typography-paragraph v-for="spaceLevel in spaceLevelList" class="card-text">
            {{ spaceLevel.text }}：存储空间 {{ formatSize(spaceLevel.maxSize) }}，支持
            {{ spaceLevel.maxCount }} 个成员
          </a-typography-paragraph>
        </a-card>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import { message } from 'ant-design-vue'
import {
  addSpaceUsingPost,
  getSpaceVoByIdUsingGet,
  listSpaceLevelUsingGet,
  updateSpaceUsingPost,
} from '@/api/spaceController.ts'
import { useRoute, useRouter } from 'vue-router'
import {
  SPACE_LEVEL_MAP,
  SPACE_LEVEL_OPTIONS,
  SPACE_TYPE_ENUM,
  SPACE_TYPE_MAP,
} from '@/constants/space.ts'
import { formatSize } from '../utils'

const space = ref<API.SpaceVO>()
const spaceForm = reactive<API.SpaceAddRequest | API.SpaceEditRequest>({})
const loading = ref(false)

const route = useRoute()
// 空间类别，默认为私有空间
const spaceType = computed(() => {
  if (route.query?.type) {
    return Number(route.query.type)
  } else {
    return SPACE_TYPE_ENUM.PRIVATE
  }
})

const spaceLevelList = ref<API.SpaceLevel[]>([])

// 获取空间级别
const fetchSpaceLevelList = async () => {
  const res = await listSpaceLevelUsingGet()
  if (res.data.code === 0 && res.data.data) {
    spaceLevelList.value = res.data.data
  } else {
    message.error('获取空间级别失败，' + res.data.message)
  }
}

onMounted(() => {
  fetchSpaceLevelList()
})

const router = useRouter()

/**
 * 提交表单
 * @param values
 */
const handleSubmit = async (values: any) => {
  const spaceId = space.value?.id
  loading.value = true
  let res
  if (spaceId) {
    // 更新
    res = await updateSpaceUsingPost({
      id: spaceId,
      ...spaceForm,
    })
  } else {
    // 创建
    res = await addSpaceUsingPost({
      ...spaceForm,
      spaceType: spaceType.value,
    })
  }
  // 操作成功
  if (res.data.code === 0 && res.data.data) {
    message.success('操作成功')
    // 跳转到空间详情页
    router.push({
      path: `/space/${res.data.data}`,
    })
  } else {
    message.error('操作失败，' + res.data.message)
  }
  loading.value = false
}

// 获取老数据
const getOldSpace = async () => {
  // 获取到 id
  const id = route.query?.id
  if (id) {
    const res = await getSpaceVoByIdUsingGet({
      id,
    })
    if (res.data.code === 0 && res.data.data) {
      const data = res.data.data
      space.value = data
      // 填充表单
      spaceForm.spaceName = data.spaceName
      spaceForm.spaceLevel = data.spaceLevel
    }
  }
}

onMounted(() => {
  getOldSpace()
})
</script>

<style scoped>
.magic-container {
  position: relative;
  min-height: 100vh;
  overflow: hidden;
  background: linear-gradient(135deg, #f8f9fa 0%, #e9ecef 100%);
}

.magic-bg {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 2;
}

.gradient-circle {
  position: absolute;
  border-radius: 50%;
  filter: blur(80px);
  opacity: 0.4;
  animation: float 20s infinite linear;
}

.gradient-circle.pink {
  width: 400px;
  height: 400px;
  background: radial-gradient(circle at 30% 30%, #ff6b6b, #ff8e8e);
  top: 20%;
  left: 10%;
}

.gradient-circle.blue {
  width: 350px;
  height: 350px;
  background: radial-gradient(circle at 70% 30%, #74b9ff, #a29bfe);
  top: 50%;
  right: 15%;
  animation-delay: 5s;
}

.gradient-circle.purple {
  width: 300px;
  height: 300px;
  background: radial-gradient(circle at 50% 70%, #a29bfe, #6c5ce7);
  bottom: 10%;
  left: 30%;
  animation-delay: 10s;
}

@keyframes float {
  0%,
  100% {
    transform: translate(0, 0) scale(1);
  }
  25% {
    transform: translate(100px, 50px) scale(1.1);
  }
  50% {
    transform: translate(-50px, 100px) scale(0.9);
  }
  75% {
    transform: translate(-100px, -50px) scale(1.05);
  }
}

.form-container {
  position: relative;
  z-index: 3;
  padding: 40px 20px;
}

.form-card {
  max-width: 680px;
  margin: 0 auto;
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(12px);
  border-radius: 16px;
  box-shadow: 0 8px 32px rgba(31, 38, 135, 0.15);
  padding: 40px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  animation: formEntrance 0.6s cubic-bezier(0.4, 0, 0.2, 1);
}

.form-header {
  text-align: center;
  margin-bottom: 32px;
}

.form-title {
  font-size: 24px;
  color: #2d3436;
  margin-bottom: 8px;
  font-weight: 600;
  letter-spacing: 1px;
}

.title-line {
  width: 60px;
  height: 3px;
  background: linear-gradient(90deg, #74b9ff 0%, #a29bfe 100%);
  margin: 0 auto;
  border-radius: 2px;
}

.form-content {
  margin-bottom: 40px;
}

.form-item {
  margin-bottom: 24px;
}

.form-label {
  font-weight: 500;
  color: #636e72;
  font-size: 14px;
}

:deep(.custom-input) {
  height: 44px;
  border-radius: 8px;
  border: 1px solid #dfe6e9;
  transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
  padding: 0 16px;
  background: rgba(255, 255, 255, 0.9);
}

:deep(.custom-input:hover) {
  border-color: #74b9ff;
  box-shadow: 0 4px 12px rgba(116, 185, 255, 0.2);
  transform: translateY(-2px);
}

:deep(.custom-select .ant-select-selector) {
  height: 44px !important;
  border-radius: 8px !important;
  border: 1px solid #dfe6e9 !important;
  padding: 0 16px !important;
  background: rgba(255, 255, 255, 0.9) !important;
}

.submit-btn {
  width: 100%;
  height: 46px;
  font-size: 16px;
  font-weight: 500;
  border-radius: 8px;
  background: linear-gradient(135deg, #74b9ff 0%, #a29bfe 100%);
  border: none;
  position: relative;
  overflow: hidden;
  transition: all 0.4s cubic-bezier(0.4, 0, 0.2, 1);
}

.submit-btn::after {
  content: '';
  position: absolute;
  top: -50%;
  left: -50%;
  width: 200%;
  height: 200%;
  background: linear-gradient(45deg, transparent, rgba(255, 255, 255, 0.3), transparent);
  transform: rotate(45deg);
  animation: btnShine 3s infinite;
}

@keyframes btnShine {
  0% {
    left: -50%;
  }
  100% {
    left: 150%;
  }
}

.info-card {
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.3);
  background: rgba(255, 255, 255, 0.9);
  transition: transform 0.3s ease;
}

.info-card:hover {
  transform: translateY(-5px);
  box-shadow: 0 6px 16px rgba(116, 185, 255, 0.15);
}

.card-title {
  font-size: 18px;
  color: #2d3436;
  display: flex;
  align-items: center;
  gap: 8px;
}

.card-text {
  color: #636e72;
  line-height: 1.6;
  margin-bottom: 12px;
}

.contact-link {
  color: #74b9ff;
  font-weight: 500;
  transition: color 0.3s;
}

@keyframes formEntrance {
  from {
    opacity: 0;
    transform: translateY(20px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

@media (max-width: 768px) {
  .form-card {
    padding: 24px;
    margin: 0 16px;
  }

  .form-title {
    font-size: 20px;
  }

  .gradient-circle {
    filter: blur(60px);
    animation-duration: 25s;
  }
}
</style>

